package com.daoImpl;

import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.LockMode;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.bean.ApplicablePart;
import com.bean.ProcureProduct;
import com.bean.ProductPart;
import com.bean.SuinRegister;
import com.dao.SuinRegisterDao;

/**
 * A data access object (DAO) providing persistence and search support for
 * SuinRegister entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.bean.SuinRegister
 * @author MyEclipse Persistence Tools
 */
public class SuinRegisterDaoImpl extends HibernateDaoSupport implements SuinRegisterDao {
	private static final Logger log = LoggerFactory
			.getLogger(SuinRegisterDaoImpl.class);
	// property constants
	public static final String SUIN_NAME = "suinName";
	public static final String SUIN_PASS = "suinPass";
	public static final String REAL_NAME = "realName";
	public static final String SUIN_CODE = "suinCode";
	public static final String ADDRESS = "address";
	public static final String PHONE = "phone";
	public static final String EMAIL = "email";
	public static final String STATE = "state";
	public static final String LOGIN_COUNT = "loginCount";

	protected void initDao() {
		// do nothing
	}

	/* (non-Javadoc)
	 * @see com.bean.SuinRegisterDao#save(com.bean.SuinRegister)
	 */
	@Override
	public void save(SuinRegister transientInstance) {
		log.debug("saving SuinRegister instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.bean.SuinRegisterDao#delete(com.bean.SuinRegister)
	 */
	@Override
	public void delete(SuinRegister persistentInstance) {
		log.debug("deleting SuinRegister instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.bean.SuinRegisterDao#findById(java.lang.Integer)
	 */
	@Override
	public SuinRegister findById(java.lang.Integer id) {
		log.debug("getting SuinRegister instance with id: " + id);
		try {
			SuinRegister instance = (SuinRegister) getHibernateTemplate().get(
					"com.bean.SuinRegister", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.bean.SuinRegisterDao#findByExample(com.bean.SuinRegister)
	 */
	@Override
	public List findByExample(SuinRegister instance) {
		log.debug("finding SuinRegister instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.bean.SuinRegisterDao#findByProperty(java.lang.String, java.lang.Object)
	 */
	@Override
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding SuinRegister instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from SuinRegister as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.bean.SuinRegisterDao#findBySuinName(java.lang.Object)
	 */
	@Override
	public List findBySuinName(Object suinName) {
		return findByProperty(SUIN_NAME, suinName);
	}

	/* (non-Javadoc)
	 * @see com.bean.SuinRegisterDao#findBySuinPass(java.lang.Object)
	 */
	@Override
	public List findBySuinPass(Object suinPass) {
		return findByProperty(SUIN_PASS, suinPass);
	}

	/* (non-Javadoc)
	 * @see com.bean.SuinRegisterDao#findByRealName(java.lang.Object)
	 */
	@Override
	public List findByRealName(Object realName) {
		return findByProperty(REAL_NAME, realName);
	}

	/* (non-Javadoc)
	 * @see com.bean.SuinRegisterDao#findBySuinCode(java.lang.Object)
	 */
	@Override
	public List findBySuinCode(Object suinCode) {
		return findByProperty(SUIN_CODE, suinCode);
	}

	/* (non-Javadoc)
	 * @see com.bean.SuinRegisterDao#findByAddress(java.lang.Object)
	 */
	@Override
	public List findByAddress(Object address) {
		return findByProperty(ADDRESS, address);
	}

	/* (non-Javadoc)
	 * @see com.bean.SuinRegisterDao#findByPhone(java.lang.Object)
	 */
	@Override
	public List findByPhone(Object phone) {
		return findByProperty(PHONE, phone);
	}

	/* (non-Javadoc)
	 * @see com.bean.SuinRegisterDao#findByEmail(java.lang.Object)
	 */
	@Override
	public List findByEmail(Object email) {
		return findByProperty(EMAIL, email);
	}

	/* (non-Javadoc)
	 * @see com.bean.SuinRegisterDao#findByState(java.lang.Object)
	 */
	@Override
	public List findByState(Object state) {
		return findByProperty(STATE, state);
	}

	/* (non-Javadoc)
	 * @see com.bean.SuinRegisterDao#findByLoginCount(java.lang.Object)
	 */
	@Override
	public List findByLoginCount(Object loginCount) {
		return findByProperty(LOGIN_COUNT, loginCount);
	}

	/* (non-Javadoc)
	 * @see com.bean.SuinRegisterDao#findAll()
	 */
	@Override
	public List findAll() {
		log.debug("finding all SuinRegister instances");
		try {
			String queryString = "from SuinRegister";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.bean.SuinRegisterDao#merge(com.bean.SuinRegister)
	 */
	@Override
	public SuinRegister merge(SuinRegister detachedInstance) {
		log.debug("merging SuinRegister instance");
		try {
			SuinRegister result = (SuinRegister) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.bean.SuinRegisterDao#attachDirty(com.bean.SuinRegister)
	 */
	@Override
	public void attachDirty(SuinRegister instance) {
		log.debug("attaching dirty SuinRegister instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.bean.SuinRegisterDao#attachClean(com.bean.SuinRegister)
	 */
	@Override
	public void attachClean(SuinRegister instance) {
		log.debug("attaching clean SuinRegister instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
	
	/**
	 * 查找用户名是否可用
	 * @param ctx
	 * @return
	 */
	public Boolean getName(String name){
		Session session = getSession();  
		SQLQuery query=null;
		
		//sql语句
		String sql = "SELECT * FROM audit.suin_register s WHERE s.suin_name=:name";
		
		try {
			
			query=session.createSQLQuery(sql);
			query.setString("name", name);
			List list = query.list();
			
			//判断是否有重复
			if(list.size()>0){
				return false;
			
			}else {
				return true;
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} 
		
	}
	
	/**
	 * 根据用户名
	 * @param ctx
	 * @return
	 */
	public SuinRegister findSuinName(String suinName){
		
		Session session = getSession();  
		SQLQuery query=null;
		
		//sql语句
		String sql = "SELECT * FROM audit.suin_register s WHERE s.state=1  and s.suin_name=:name";
			
		query=session.createSQLQuery(sql);
		query.setString("name", suinName);
		query.addEntity(SuinRegister.class);
		List list = query.list();
			
		SuinRegister suinRegister=(SuinRegister)list.get(0);
	
		
		return suinRegister;
		
	}
	
		@Override
	public List<SuinRegister> getFindSuinRegister(String table, String pk,String sort,
			int PageNumber, int PageSize, String options) {
		Session session = getSession();
		SQLQuery query = null;
		
		StringBuffer sqlbuf = new StringBuffer("execute [dbo].[sp_get_page_of_table2] ");
		sqlbuf.append("'"+table+"','"+pk+"','"+sort+"',"+PageNumber+","+PageSize+",'*',\""+options+"\"");
		
		query=session.createSQLQuery(sqlbuf.toString());
		query.addEntity(SuinRegister.class);
		
		List<SuinRegister> list = query.list();
		
		return list;

	}
	
	public static SuinRegisterDao getFromApplicationContext(
			ApplicationContext ctx) {
		return (SuinRegisterDao) ctx.getBean("SuinRegisterDao");
	}
}